/*
 * Copyright (c) 2023 iQi.Co.Ltd. All rights reserved.
 */
 
using System.Collections.Generic;
using NetProtocol.POD;
using NetProtocol.Enum;
using IQIGame.Onigao.Game;
using IQIGame.Onigao.GamePlay;
using IQIGame.Onigao.Framework;
using System.Threading.Tasks;
using System;

namespace NetProtocol.Server {
	/// <summary>
	/// 战斗
	/// module : battle
	/// </summary>
	public interface SNetBattle : INetServerModule {
		Dictionary<int, MessageHandler> INetServerModule.GetModuleFunction()
        {	
            return new()
            {
                { NetMessageId.battle_localNotifyBattleStart, (session, data) =>
		            {
#if ENABLE_NET_LOG || UNITY_EDITOR
						NetProtocolHelper.ServerLog(NetMessageId.battle_localNotifyBattleStart, "Recv->" + data.ToString());
#endif
		                LocalNotifyBattleStartRequest _data = (LocalNotifyBattleStartRequest)data;
		                CS_localNotifyBattleStart(session, _data.battleId);
		                return VoidMessageResult.Instance;
		            } 
            	},
                { NetMessageId.battle_localNotifyBattleEnd, (session, data) =>
		            {
#if ENABLE_NET_LOG || UNITY_EDITOR
						NetProtocolHelper.ServerLog(NetMessageId.battle_localNotifyBattleEnd, "Recv->" + data.ToString());
#endif
		                LocalNotifyBattleEndRequest _data = (LocalNotifyBattleEndRequest)data;
		                CS_localNotifyBattleEnd(session, _data.battleEndPOD);
		                return VoidMessageResult.Instance;
		            } 
            	},
                { NetMessageId.battle_localNotifyBattleExit, (session, data) =>
		            {
#if ENABLE_NET_LOG || UNITY_EDITOR
						NetProtocolHelper.ServerLog(NetMessageId.battle_localNotifyBattleExit, "Recv->" + data.ToString());
#endif
		                LocalNotifyBattleExitRequest _data = (LocalNotifyBattleExitRequest)data;
		                CS_localNotifyBattleExit(session, _data.battleId);
		                return VoidMessageResult.Instance;
		            } 
            	},
            };
        }
        
        Dictionary<int, Func<IReceiveMessage>> INetServerModule.GetModuleRequest() 
        {
	        return new ()
	        {
	        	{NetMessageId.battle_localNotifyBattleStart, ()=> new LocalNotifyBattleStartRequest()},
	        	{NetMessageId.battle_localNotifyBattleEnd, ()=> new LocalNotifyBattleEndRequest()},
	        	{NetMessageId.battle_localNotifyBattleExit, ()=> new LocalNotifyBattleExitRequest()},
	        };
	    }
        
#region Server-side Interface
		/// <summary>
        ///  通知战斗开始
        /// </summary>
    	protected bool CS_localNotifyBattleStart(ISession session, long battleId);
		/// <summary>
        ///  通知战斗结果
        /// </summary>
    	protected bool CS_localNotifyBattleEnd(ISession session, NotifyBattleEndPOD battleEndPOD);
		/// <summary>
        ///  通知退出战斗
        /// </summary>
    	protected bool CS_localNotifyBattleExit(ISession session, long battleId);
#endregion
#region Client-side Interface
		/// <summary>
        ///  通知创建本地战斗，并直接进入
        /// </summary>
		public bool SC_localNotifyCreateBattle(ISession session, LogicPlayerPOD playerPOD, CreateBattlePOD battlePOD){
	        return NetProtocolHelper.SendMessage(session, new LocalNotifyCreateBattle(playerPOD, battlePOD));
	    }
	    public void SC_localNotifyCreateBattle_Broadcast(List<ISession> sessions, LogicPlayerPOD playerPOD, CreateBattlePOD battlePOD){
	        NetProtocolHelper.BroadcastMessage(sessions, new LocalNotifyCreateBattle(playerPOD, battlePOD));
	    }
		/// <summary>
        ///  通知退出本地战斗(强制踢出)
        /// </summary>
		public bool SC_localNotifyShutdownBattle(ISession session, long battleId){
	        return NetProtocolHelper.SendMessage(session, new LocalNotifyShutdownBattle(battleId));
	    }
	    public void SC_localNotifyShutdownBattle_Broadcast(List<ISession> sessions, long battleId){
	        NetProtocolHelper.BroadcastMessage(sessions, new LocalNotifyShutdownBattle(battleId));
	    }
		/// <summary>
        ///  通知战斗结算
        /// </summary>
		public bool SC_notifyBattleSettlement(ISession session, BattleSettlementPOD battleSettlementPOD){
	        return NetProtocolHelper.SendMessage(session, new NotifyBattleSettlement(battleSettlementPOD));
	    }
	    public void SC_notifyBattleSettlement_Broadcast(List<ISession> sessions, BattleSettlementPOD battleSettlementPOD){
	        NetProtocolHelper.BroadcastMessage(sessions, new NotifyBattleSettlement(battleSettlementPOD));
	    }
#endregion

#region Messages
	public class LocalNotifyCreateBattle : MessageNotify {
		private LogicPlayerPOD playerPOD;
		private CreateBattlePOD battlePOD;
		public LocalNotifyCreateBattle(LogicPlayerPOD playerPOD, CreateBattlePOD battlePOD) {
		    this.playerPOD = playerPOD;
		    this.battlePOD = battlePOD;
		}
		public override int GetMessageId() {return NetMessageId.battle_localNotifyCreateBattle;}
		public override SendPackage ToPackage() {
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.battle_localNotifyCreateBattle;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_pod(playerPOD);
			_out.Write_pod(battlePOD);
			return si_p;
		}
		public override string ToString() {
			return "Battle.SC_localNotifyCreateBattle { "
			+ "playerPOD="
			+ playerPOD
			+ ", battlePOD="
			+ battlePOD
			+" }";
		}
	}
	public class LocalNotifyShutdownBattle : MessageNotify {
		private long battleId;
		public LocalNotifyShutdownBattle(long battleId) {
		    this.battleId = battleId;
		}
		public override int GetMessageId() {return NetMessageId.battle_localNotifyShutdownBattle;}
		public override SendPackage ToPackage() {
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.battle_localNotifyShutdownBattle;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_long(battleId);
			return si_p;
		}
		public override string ToString() {
			return "Battle.SC_localNotifyShutdownBattle { "
			+ "battleId="
			+ battleId
			+" }";
		}
	}
	public class NotifyBattleSettlement : MessageNotify {
		private BattleSettlementPOD battleSettlementPOD;
		public NotifyBattleSettlement(BattleSettlementPOD battleSettlementPOD) {
		    this.battleSettlementPOD = battleSettlementPOD;
		}
		public override int GetMessageId() {return NetMessageId.battle_notifyBattleSettlement;}
		public override SendPackage ToPackage() {
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.battle_notifyBattleSettlement;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_pod(battleSettlementPOD);
			return si_p;
		}
		public override string ToString() {
			return "Battle.SC_notifyBattleSettlement { "
			+ "battleSettlementPOD="
			+ battleSettlementPOD
			+" }";
		}
	}
	private class LocalNotifyBattleStartRequest : IReceiveMessage
    {
		internal long battleId;
        public void FromPackage(ReceivePackage package)
		{
			SInputStream _in = package.InputStream;
            battleId = _in.HasRemaining() ? _in.Read_long() : default;
		}
        public int GetMessageId() { return NetMessageId.battle_localNotifyBattleStart; }
        public override string ToString()
        {
            return "Battle.localNotifyBattleStart"+ ", battleId="+ battleId;
        }
    }
	private class LocalNotifyBattleEndRequest : IReceiveMessage
    {
		internal NotifyBattleEndPOD battleEndPOD;
        public void FromPackage(ReceivePackage package)
		{
			SInputStream _in = package.InputStream;
            battleEndPOD = _in.HasRemaining() ? _in.Read_pod<NotifyBattleEndPOD>(NotifyBattleEndPOD.DynamicFactory) : default;
		}
        public int GetMessageId() { return NetMessageId.battle_localNotifyBattleEnd; }
        public override string ToString()
        {
            return "Battle.localNotifyBattleEnd"+ ", battleEndPOD="+ battleEndPOD;
        }
    }
	private class LocalNotifyBattleExitRequest : IReceiveMessage
    {
		internal long battleId;
        public void FromPackage(ReceivePackage package)
		{
			SInputStream _in = package.InputStream;
            battleId = _in.HasRemaining() ? _in.Read_long() : default;
		}
        public int GetMessageId() { return NetMessageId.battle_localNotifyBattleExit; }
        public override string ToString()
        {
            return "Battle.localNotifyBattleExit"+ ", battleId="+ battleId;
        }
    }
	}
#endregion
}